package com.zhengtoon.mctoon.conv.controller;

import com.bjtoon.framework.entity.ResponseResult;
import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.dto.CommentInfoDto;
import com.zhengtoon.mctoon.conv.dto.ResultCommentDto;
import com.zhengtoon.mctoon.conv.dto.ResultPraisConvPhotoDto;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.utils.AppConstant;
import com.zhengtoon.mctoon.conv.utils.SessionInfo;
import com.zhengtoon.mctoon.conv.utils.XssEncodeUtils;
import com.zhengtoon.mctoon.conv.service.ConvCommentService;
import com.zhengtoon.mctoon.conv.service.ConvPhotoInfoService;
import com.zhengtoon.mctoon.conv.service.ConvPraiseInfoService;
import com.zhengtoon.mctoon.conv.vo.CommentPage;
import com.zhengtoon.mctoon.conv.vo.ConvPhotoVo;
import com.zhengtoon.mctoon.conv.vo.PageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/praisingCity")
@Api(value = "praisingCity服务", description = "praisingCity服务")
public class PraisingCityController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(PraisingCityController.class);

    @Autowired
    private ConvPraiseInfoService convPraiseInfoService;

    @Autowired
    private ConvPhotoInfoService convPhotoInfoService;

    @Autowired
    private ConvCommentService convCommentService;


    /**
     * 获取大赞北京列表
     *
     * @param pageVo 分页参数
     * @return ResponseResult
     */
    @ApiOperation(value = "获取大赞北京列表", httpMethod = "POST", notes = "获取大赞北京列表")
    @RequestMapping(value = "/getPhotoList", method = RequestMethod.POST)
    public ResponseResult getPhotoList(HttpServletRequest request,
                                       @ApiParam(required = true, name = "pageVo", value = "分页参数") @RequestBody @Valid PageVo pageVo) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }
            if (StringUtils.isAnyBlank(pageVo.getPageSize(), pageVo.getPageNum())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }

            List<ResultPraisConvPhotoDto> rs = convPhotoInfoService.queryPraisPhotoList(pageVo.getPageNum(), pageVo.getPageSize(), sessionInfo.getUserId(), 1);
            return ResponseResult.success(rs);
        } catch (Exception e) {
            log.error("获取大赞北京列表的方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "获取大赞北京列表的方法error");
        }
    }

    /**
     * 点赞
     *
     * @param convPhotoVo 随手拍记录id
     * @return ResponseResult
     */
    @ApiOperation(value = "点赞 ", httpMethod = "POST", notes = "点赞")
    @RequestMapping(value = "/praise", method = RequestMethod.POST)
    public ResponseResult praise(HttpServletRequest request,
                                 @ApiParam(required = true, name = "convPhotoVo", value = "随手拍记录id") @RequestBody @Valid ConvPhotoVo convPhotoVo) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }
            if (StringUtils.isBlank(convPhotoVo.getId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }

            int rs = convPraiseInfoService.praise(convPhotoVo.getId(), sessionInfo.getUserId());
            if (rs == 0) {
                return new ResponseResult(ConvPhotoExceptionCode.RESULT_IS_NULL);
            }
            return ResponseResult.success(rs);
        } catch (Exception e) {
            log.error("点赞方法error:" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "点赞方法error");
        }
    }


    /**
     * 提交评论
     *
     * @param commentInfoDto 评论对象
     * @return ResponseResult
     */
    @ApiOperation(value = "提交评论 ", httpMethod = "POST", notes = "提交评论")
    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    public ResponseResult addComment(HttpServletRequest request,
                                     @ApiParam(required = true, name = "commentInfoDto", value = "评论对象") @RequestBody @Valid CommentInfoDto commentInfoDto) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }
            commentInfoDto.setContent(XssEncodeUtils.xssEncode(commentInfoDto.getContent()));
            commentInfoDto.setUserId(sessionInfo.getUserId());
            log.debug("提交评论参数：" + commentInfoDto.toString());
            int rs = convCommentService.addComment(commentInfoDto);
            if (rs == 0) {
                return new ResponseResult(ConvPhotoExceptionCode.RESULT_IS_NULL);
            }
            return ResponseResult.success("评论成功");
        } catch (BusinessException e1) {
            return new ResponseResult<>(AppConstant.ERROR_CODE, e1.getMessage());
        } catch (Exception e) {
            log.error("提交评论方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "提交评论失败");
        }
    }

    /**
     * 获取随手拍评论列表
     *
     * @param commentPage 评论对象
     * @return ResponseResult
     */
    @ApiOperation(value = "获取随手拍评论列表 ", httpMethod = "POST", notes = "获取随手拍评论列表")
    @RequestMapping(value = "/queryCommentListById", method = RequestMethod.POST)
    public ResponseResult queryCommentListById(HttpServletRequest request,
                                               @ApiParam(required = true, name = "commentPage", value = "评论对象") @RequestBody @Valid CommentPage commentPage) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }
            if (StringUtils.isAnyBlank(commentPage.getId(), commentPage.getPageSize(), commentPage.getPageNum())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }
            List<ResultCommentDto> rs = convCommentService.queryCommentListById(commentPage.getId(), commentPage.getPageNum(), commentPage.getPageSize(), sessionInfo.getUserId());
            return ResponseResult.success(rs);
        } catch (Exception e) {
            log.error("获取我的随手拍评论列表方法error:" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "获取我的随手拍评论列表方法error");
        }
    }

}